package questionmanager;

import gui.GUIMain;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import rmi.Question;

public class GUIQuestionMove extends JDialog{
	
	private static final long serialVersionUID = 1L;
	private ArrayList<Integer> qIDList;
	private int typeCount;
	private Integer[] typeInt;
	private JButton btOK;
	private JButton btCancel;
	
	public GUIQuestionMove(JFrame parent, ArrayList<Integer> qIDList) {
		super(parent, "Chuyển câu hỏi", true);
		this.qIDList = qIDList;
		setLayout(new BorderLayout());
		
		String[] typeString;
		try {
			ArrayList<String> rstemp = new ArrayList<>();
			ArrayList<Integer> rxtemp = new ArrayList<>();
			LinkedHashMap<Integer,String> temp = GUIMain.getDb().getAllQuestionTypes();
			typeString = new String[temp.size()];
			typeInt = new Integer[temp.size()];
			
			for(Entry<Integer, String> count : temp.entrySet()){
				rxtemp.add(count.getKey());
				rstemp.add(count.getValue());
			}
			rxtemp.toArray(typeInt);
			rstemp.toArray(typeString);
			
		} catch (SQLException e) {
			e.printStackTrace();
			return;
		}
		
		final JComboBox<String> type = new JComboBox<>(typeString); 
		type.setSelectedIndex(0);
		type.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				typeCount = type.getSelectedIndex();
			}
		});
		JPanel contain = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridx=0;
		c.gridy=0;
		c.weightx = 1;
		c.ipadx = 5;
		c.ipady = 5;
		c.insets = new Insets(10, 10, 10, 10);
		contain.add(type,c);
		add(contain, BorderLayout.CENTER);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		btOK = new JButton(new ButtonOKAction());
		btOK.setText("Thêm");
		btCancel = new JButton(new ButtonCancelAction());
		btCancel.setText("Thoát");
		buttonPanel.add(btOK);
		buttonPanel.add(btCancel);
		add(buttonPanel,BorderLayout.SOUTH);
		
	}
	
	class ButtonOKAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			for(Integer count : qIDList){
				Question ex;
				try {
					ex = GUIMain.getDb().getQuestionsByQuestionID(count.intValue());
					GUIMain.getDb().UpdateQuestion(ex.getQuestionID(), ex.getQuestion(),ex.getQuestionImageLink(), typeInt[typeCount], ex.getPriority());
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(GUIQuestionMove.this, "Lỗi cập nhật câu hỏi vào cơ sở dữ liệu",
							"Lỗi", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
			JOptionPane.showMessageDialog(GUIQuestionMove.this, "Chuyển danh sách câu hỏi thành công",
					"Thành công", JOptionPane.ERROR_MESSAGE);
			dispose();
		}
		
	}
	
	class ButtonCancelAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			dispose();
		}
		
	}
}
